﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using AnnotateMe.Interfaces;
using System.Drawing;

namespace AnnotateMe.Logic
{
    public class SimpleAnnotator
    {
        public IEnumerable<PossiblyAnnotatedBlock> CreateBlocks(
            IEnumerable<Tuple<string, string, Color?>> inputs, 
            Font textFont, 
            Font annotationFont,
            Graphics graphics)
        {
            double originX = 50;
            double originY = 100;

            foreach (var input in inputs)
            {
                var textRectangleOrigin = new ImmutablePoint(originX, originY);
                var textSize = graphics.MeasureString(input.Item1, textFont);
                var textRectangleSize = new ImmutableSize(textSize.Width, textSize.Height);
                var color = input.Item3;
                var text = input.Item1;
                var textRectangle = new ImmutableRectangle(
                    textRectangleOrigin, 
                    textRectangleSize, 
                    (rect) => SimpleAnnotator.DrawTextBlock(graphics, text, textFont, color, rect));

                if (string.IsNullOrWhiteSpace(input.Item2))
                {
                    yield return new PossiblyAnnotatedBlock(textRectangle);
                }
                else
                {
                    var annotationRectangleOrigin = new ImmutablePoint(0, 0);
                    var annotationSize = graphics.MeasureString(input.Item2, annotationFont);
                    var annotationRectangleSize = new ImmutableSize(annotationSize.Width, annotationSize.Height);
                    var annotationText = input.Item2;

                    var annotationRectangle = new ImmutableRectangle(
                        annotationRectangleOrigin,
                        annotationRectangleSize,
                        (rect) => SimpleAnnotator.DrawTextBlock(graphics, annotationText, annotationFont, null, rect));

                    yield return new PossiblyAnnotatedBlock(textRectangle, annotationRectangle);
                }

                originX += textRectangle.Size.Width;
            }
        }

        public static void DrawTextBlock(Graphics g, string text, Font font, Color? highlightColor, ImmutableRectangle rect)
        {
            //draw label
            g.DrawString(text, font, Brushes.Black, rect.OriginPoint.ToPointF());    

            //draw rectangle
            if(highlightColor.HasValue)
            {
                var color = highlightColor.Value;

                using(var pen = new Pen(color, 2.0f))
                {
                    using(var brush = new SolidBrush(Color.FromArgb(30, color)))
                    {
                        g.DrawRectangle(pen, rect.ToRectangle());
                        g.FillRectangle(brush, rect.ToRectangle());
                    }
                }
            }
        }
    }
}
